package com.ostrich.a2104shopping.ui.home;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.blankj.utilcode.util.ToastUtils;
import com.ostrich.a2104shopping.R;
import com.ostrich.a2104shopping.databinding.ActivityCarBinding;
import com.ostrich.a2104shopping.ui.home.adapter.CarAdapter;
import com.ostrich.a2104shopping.ui.home.entity.AddCarRequestEntity;
import com.ostrich.a2104shopping.ui.home.entity.CarEntity;
import com.ostrich.a2104shopping.ui.home.entity.IdsRequestEntity;
import com.ostrich.a2104shopping.ui.home.viewmodel.CarViewModel;
import com.ostrich.lib_common.base.BaseMvvmActivity;
import com.ostrich.lib_common.net.BaseResponseEntity;

import java.util.List;

//查看购物车
public class CarActivity extends BaseMvvmActivity<CarViewModel, ActivityCarBinding> {
    private CarAdapter mCarAdapter;

    @Override
    public int bindLayout() {
        return R.layout.activity_car;
    }

    @Override
    public void initView() {
        mCarAdapter = new CarAdapter();
        binding.rv.setAdapter(mCarAdapter);
        binding.rv.setLayoutManager(new LinearLayoutManager(this));
        //分割线
        binding.rv.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
    }

    @Override
    public void initData() {
        viewModel.selectCar().observe(this, new Observer<BaseResponseEntity<List<CarEntity>>>() {
            @Override
            public void onChanged(BaseResponseEntity<List<CarEntity>> listBaseResponseEntity) {
                if(listBaseResponseEntity.getCode() == 200){
                    List<CarEntity> data = listBaseResponseEntity.getData();
                    mCarAdapter.refreshData(data);
                }
            }
        });

    }

    @Override
    public void initListener() {
        mCarAdapter.setItemChildClickListener(new CarAdapter.ItemChildClickListener() {
            @Override
            public void onItemChildClick(View v, int position) {
                switch (v.getId()){

                    case R.id.iv_remove://删除
                    {
                        IdsRequestEntity idsRequestEntity = new IdsRequestEntity();
                        idsRequestEntity.setIds(mCarAdapter.getDataByPosition(position).getId()+"");
                        viewModel.deleteCar(idsRequestEntity).observe(CarActivity.this, new Observer<BaseResponseEntity<String>>() {
                            @Override
                            public void onChanged(BaseResponseEntity<String> stringBaseResponseEntity) {
                                if(stringBaseResponseEntity.getCode() == 200){
                                    ToastUtils.showShort("删除成功");
                                    mCarAdapter.deleteData(position);
                                }else{
                                    ToastUtils.showShort("删除失败");
                                }
                            }
                        });

                        total();

                    }
                        break;
                    case R.id.cb:
                    {
                        //修改本地数据源的状态
                        CheckBox checkBox = (CheckBox) v;
                        mCarAdapter.getDataByPosition(position).setCheck(checkBox.isChecked());
                        total();
                    }
                        break;
                    case R.id.iv_add://数量+
                    {
                        //先本地+1
                        CarEntity carEntity = mCarAdapter.getDataByPosition(position);
                        int count = carEntity.getCount();
                        count++;
                        carEntity.setCount(count);
                       // mCarAdapter.notifyItemRangeChanged(position,1);
                        mCarAdapter.notifyItemChanged(position,"num");//局部增量刷新
                        //网络请求
                        AddCarRequestEntity addCarRequestEntity = new AddCarRequestEntity();
                        addCarRequestEntity.setCount(1);
                        addCarRequestEntity.setGoods_id(carEntity.getGoods_id());
                        viewModel.addCar(addCarRequestEntity).observe(CarActivity.this, new Observer<BaseResponseEntity>() {
                            @Override
                            public void onChanged(BaseResponseEntity baseResponseEntity) {
                                if(baseResponseEntity.getCode() == 200){

                                }else{
                                    ToastUtils.showShort("数量+失败");
                                    //本地-1
                                    CarEntity carEntity = mCarAdapter.getDataByPosition(position);
                                    int count = carEntity.getCount();
                                    count--;
                                    carEntity.setCount(count);
                                   // mCarAdapter.notifyItemRangeChanged(position,1);
                                    mCarAdapter.notifyItemChanged(position,"num");//局部增量刷新
                                }
                            }
                        });
                        total();

                    }

                        break;
                    case R.id.iv_stub://数量-
                    {
                        //先本地+1
                        CarEntity carEntity = mCarAdapter.getDataByPosition(position);
                        int count = carEntity.getCount();
                        count--;
                        carEntity.setCount(count);
                        // mCarAdapter.notifyItemRangeChanged(position,1);
                        mCarAdapter.notifyItemChanged(position,"num");//局部增量刷新
                        //网络请求
                        AddCarRequestEntity addCarRequestEntity = new AddCarRequestEntity();
                        addCarRequestEntity.setCount(1);
                        addCarRequestEntity.setGoods_id(carEntity.getGoods_id());
                        viewModel.addCar(addCarRequestEntity).observe(CarActivity.this, new Observer<BaseResponseEntity>() {
                            @Override
                            public void onChanged(BaseResponseEntity baseResponseEntity) {
                                if(baseResponseEntity.getCode() == 200){

                                }else{
                                    ToastUtils.showShort("数量+失败");
                                    //本地-1
                                    CarEntity carEntity = mCarAdapter.getDataByPosition(position);
                                    int count = carEntity.getCount();
                                    count++;
                                    carEntity.setCount(count);
                                    // mCarAdapter.notifyItemRangeChanged(position,1);
                                    mCarAdapter.notifyItemChanged(position,"num");//局部增量刷新
                                }
                            }
                        });
                        total();

                    }
                        break;

                }
            }
        });
        //删除选中的 {"ids","1,4"}
        binding.tvDeleteCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //遍历集合 拼接字符串
                List<CarEntity> carEntities = mCarAdapter.getCarEntities();
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < carEntities.size(); i++) {
                    if(carEntities.get(i).isCheck()){
                        stringBuilder.append(carEntities.get(i).getId());
                        if(i != carEntities.size() -1){
                            stringBuilder.append(",");
                        }
                    }

                }
                Log.d("ytx", "onClick: "+stringBuilder.toString());


                IdsRequestEntity idsRequestEntity = new IdsRequestEntity();
                idsRequestEntity.setIds(stringBuilder.toString());
                viewModel.deleteCar(idsRequestEntity).observe(CarActivity.this, new Observer<BaseResponseEntity<String>>() {
                    @Override
                    public void onChanged(BaseResponseEntity<String> stringBaseResponseEntity) {
                        if(stringBaseResponseEntity.getCode() == 200){
                            ToastUtils.showShort("删除成功");
                            mCarAdapter.deleteCheck();//删除选中

                            total();
                        }else{
                            ToastUtils.showShort("删除失败");
                        }
                    }
                });
            }
        });

        //全选和取消全选
        binding.cbAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                List<CarEntity> carEntities = mCarAdapter.getCarEntities();
                for (CarEntity carEntity : carEntities) {
                    carEntity.setCheck(isChecked);
                }
                mCarAdapter.notifyDataSetChanged();
                total();
            }
        });

    }

    @Override
    public void createViewModel() {
        viewModel = new ViewModelProvider(this).get(CarViewModel.class);
    }

    //总价
    public void total(){
        int num = 0;
        double total = 0;
        List<CarEntity> carEntities = mCarAdapter.getCarEntities();
        for (CarEntity carEntity : carEntities) {
            if(carEntity.isCheck()){
                num+=carEntity.getCount();
                total+= carEntity.getCount()*carEntity.getGoods_default_price();
            }
        }
        binding.tvTotal.setText("总价:¥"+total+"("+num+")");
    }

}